package com.kbt.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import com.kbt.activity.GoodInfoActivity;
import com.kbt.activity.R;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.listener.*;
import com.kbt.model.data.ShopInfo;
import com.kbt.util.data.StaticData;
import com.kbt.util.tool.BtnSetting;
import com.kbt.util.tool.CarCountBadgeUtil;
import com.kbt.util.tool.CommonStatus;
import com.kbt.util.tool.PopWindowSetting;
import com.kbt.util.utils.ImageManager;
import com.readystatesoftware.viewbadger.BadgeView;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class AddCarPopWindow extends PopupWindow {
    //右上角提示符
    BadgeView badge;
    //生命周期
    private Activity context;
    private ImageView closeImg;
    private Button subBtn;
    private Button addBtn;
    private EditText sumEdit;
    private Button addCarBtn;
    private Button carBtn;
    private TextView goodTitle;
    private View vi;
    private ImageView goodImg;
    private String imageName;
    private TextView car_goods_title;
    private float price;
    private String count;//库存数量

    /**
     * @param context
     * @param popFrom
     */
    public AddCarPopWindow(final Activity context, int popFrom,String id,String imageName,float price,String count) {
        //初始化信息
        super(context);
        this.context = context;
        this.imageName = imageName;
        this.price = price;
        this.count = count;

        //获取布局信息
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi = inflater.inflate(R.layout.good_add_car_layout, null);

        closeImg = (ImageView) vi.findViewById(R.id.car_close_img);
        subBtn = (Button) vi.findViewById(R.id.addCar_sub_btn);
        addBtn = (Button) vi.findViewById(R.id.addCar_add_btn);
        sumEdit = (EditText) vi.findViewById(R.id.addCar_good_count);
        car_goods_title = (TextView) vi.findViewById(R.id.car_goods_title);
        car_goods_title.setText("￥"+price+"(库存:"+count+")");

        addCarBtn = (Button) vi.findViewById(R.id.add_car_yes);
        carBtn = (Button) vi.findViewById(R.id.good_info_car);
        goodTitle = (TextView) vi.findViewById(R.id.goodTitle);

        badge = new BadgeView(context.getApplicationContext(), carBtn);
        goodImg = (ImageView) vi.findViewById(R.id.car_good_img);
        ImageManager.imageLoader.displayImage(Constants.ServiceInterFace.imageURL + imageName, goodImg, ImageManager.getOptions(
                R.drawable.loadings, R.drawable.loadings));


        //设置SelectPicPopupWindow的View
        this.setContentView(vi);
        //设置弹出窗口样式
        PopWindowSetting.setPopWindowStyle(this);

        //设置父窗体背景
        PopWindowSetting.setParentBackGround(context, 0.3f);

        //设置窗口销毁时事件
        PopWindowDismissListener dismissListener = new PopWindowDismissListener(context);
        this.setOnDismissListener(dismissListener);


        //设置按钮事件
        //取消按钮
        ClickDimsPopWindowListener closeImgOnClickListener = new ClickDimsPopWindowListener(this);
        closeImg.setOnClickListener(closeImgOnClickListener);


        //减按钮事件
        subBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Integer.parseInt(sumEdit.getText().toString())==1){
                    sumEdit.setText("1");
                }else{
                    int count =Integer.parseInt(sumEdit.getText().toString())-1;
                    sumEdit.setText(count+"");
                }
            }
        });

        //加
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = Integer.parseInt(sumEdit.getText().toString())+1;
                sumEdit.setText(count+"");
            }
        });


        //确认加入购物车

//        BtnSetting.setBtnBackground(addCarBtn, 2, Color.RED, Color.RED, 5);
        ClickAddCarListener addCarListener = new ClickAddCarListener(context, id, sumEdit, badge, popFrom,count);
        addCarBtn.setOnClickListener(addCarListener);


        //购物车
//        Map params = new HashMap();
//        params.put("currentItem", StaticData.INDEX_CAR);
//        params.put("carStatus", StaticData.GOOD_CAR_COMPLETE);
        BtnSetting.setBtnBackground(carBtn, 2, Color.GRAY, Color.WHITE, 5);
//        ClickStartActivityListener startListener = new ClickStartActivityListener(context, StaticData.GOOD_SUBMIT_CLASS, params);
        carBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.getInstance().removeActivitytwo();
                BaseApplication.getInstance().currentItem=StaticData.INDEX_CAR;
                BaseApplication.getInstance().carStatus=StaticData.GOOD_CAR_COMPLETE;
                CommonStatus.carStatus = StaticData.GOOD_CAR_COMPLETE;
                context.finish();
            }
        });

        //如果窗口由商品详情打开，获取已加入购物车的数量
        if (popFrom == StaticData.PopWindowFromGoodInfo) {
            String p_text = String.valueOf(((GoodInfoActivity) context).carBadge.getText());
            p_text = (p_text.equals("null") || p_text.trim().length() == 0) ? "0" : p_text;
            CarCountBadgeUtil.setCarBadge(badge, Integer.parseInt(p_text));
        }
    }


}
